package com.frank.oj.controller.oj;

import com.frank.oj.annotation.AnonApi;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.model.dto.ChangeEmailDTO;
import com.frank.oj.model.dto.ChangePasswordDTO;
import com.frank.oj.model.dto.CheckUsernameOrEmailDTO;
import com.frank.oj.model.vo.*;
import com.frank.oj.service.oj.AccountService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author frank
 * @Date 2024/1/25
 */
@RestController
@RequestMapping("/api")
public class AccountController {
    @Resource
    private AccountService accountService;

    /**
     * 获取用户权限信息
     */
    @GetMapping("/get-user-auth-info")
    @RequiresAuthentication
    public CommonResult<UserAuthInfoVO> getUserAuthInfo() {
        return accountService.getUserAuthInfo();
    }

    /**
     * 校验用户名和邮箱是否存在
     *
     * @param checkUsernameOrEmailDTO 用户名和邮箱
     * @return 是否存在
     */
    @PostMapping("/check-username-or-email")
    @AnonApi
    public CommonResult<CheckUsernameOrEmailVO> checkUsernameOrEmail(@RequestBody CheckUsernameOrEmailDTO checkUsernameOrEmailDTO) {
        return accountService.checkUsernameOrEmail(checkUsernameOrEmailDTO);
    }

    /**
     * 个人主页
     *
     * @param uid      用户id
     * @param username 姓名
     * @return 信息
     */
    @GetMapping("/get-user-home-info")
    public CommonResult<UserHomeVO> getUserHomeInfo(@RequestParam(value = "uid", required = false) String uid,
                                                    @RequestParam(value = "username", required = false) String username) {
        return accountService.getUserHomeInfo(uid, username);
    }

    /**
     * 热力图
     *
     * @param uid      用户id
     * @param username 姓名
     * @return 热力信息
     */
    @GetMapping("/get-user-calendar-heatmap")
    @AnonApi
    public CommonResult<UserCalendarHeatmapVO> getUserCalendarHeatmap(@RequestParam(value = "uid", required = false) String uid,
                                                                      @RequestParam(value = "username", required = false) String username) {
        return accountService.getUserCalendarHeatmap(uid, username);
    }

    /**
     * 修改密码
     */
    @PostMapping("/change-password")
    @RequiresAuthentication
    public CommonResult<ChangeAccountVO> changePassword(@RequestBody ChangePasswordDTO changePasswordDto, HttpServletRequest request) {
        return accountService.changePassword(changePasswordDto ,request);
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/change-userInfo")
    @RequiresAuthentication
    public CommonResult<UserInfoVO> changeUserInfo(@RequestBody UserInfoVO userInfoVo) {
        return accountService.changeUserInfo(userInfoVo);
    }

    /**
     * 获取邮箱验证码
     */
    @GetMapping("/get-change-email-code")
    @RequiresAuthentication
    public CommonResult<Void> getChangeEmailCode(@RequestParam("email") String email) {
        return accountService.getChangeEmailCode(email);
    }

    /**
     * 修改邮箱
     */
    @PostMapping("/change-email")
    @RequiresAuthentication
    public CommonResult<ChangeAccountVO> changeEmail(@RequestBody ChangeEmailDTO changeEmailDto) {
        return accountService.changeEmail(changeEmailDto);
    }
}
